<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - 学生知识管理系统</title>
    <link rel="shortcut icon" href="/public/favicon.ico" type="image/x-icon">
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">

    <!-- Tailwind配置 -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        dark: '#1F2937',
                        light: '#F9FAFB',
                    },
                    fontFamily: {
                        inter: ['Inter', 'sans-serif'],
                    },
                }
            }
        }
    </script>
</head>

<body class="bg-gray-50 font-inter text-dark min-h-screen flex items-center">
    <div class="container mx-auto px-4 py-8">
        <div class="max-w-md mx-auto">
            <!-- 品牌标识 -->
            <div class="text-center mb-8">
                <div class="w-16 h-16 bg-primary rounded-lg flex items-center justify-center mx-auto mb-4 shadow-md">
                    <i class="fa fa-graduation-cap text-white text-2xl"></i>
                </div>
                <h1 class="text-2xl font-bold text-dark">学生知识管理系统</h1>
            </div>

            <!-- 注册表单 -->
            <div class="bg-white rounded-xl shadow-md p-6 md:p-8">
                <h2 class="text-xl font-semibold mb-6 text-center">创建新账号</h2>

                <!-- 通用提示区域 -->
                <div id="generalMessage" class="text-center mb-4 hidden">
                    <div class="inline-flex items-center px-4 py-2 rounded-full" role="alert">
                        <span id="generalMessageText"></span>
                    </div>
                </div>

                <form id="registerForm" class="space-y-5">
                    <!-- 用户名 -->
                    <div>
                        <label for="username" class="block text-sm font-medium text-gray-700 mb-1">用户名</label>
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fa fa-user text-gray-400"></i>
                            </div>
                            <input type="text" id="username" class="pl-10 pr-4 py-3 w-full border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary/50 focus:border-primary outline-none transition" placeholder="请输入用户名" required>
                        </div>
                    </div>

                    <!-- 密码 -->
                    <div>
                        <label for="password" class="block text-sm font-medium text-gray-700 mb-1">密码</label>
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fa fa-lock text-gray-400"></i>
                            </div>
                            <input type="password" id="password" class="pl-10 pr-4 py-3 w-full border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary/50 focus:border-primary outline-none transition" placeholder="请输入密码" required>
                        </div>
                    </div>

                    <!-- 确认密码 -->
                    <div>
                        <label for="confirmPassword" class="block text-sm font-medium text-gray-700 mb-1">确认密码</label>
                        <div class="relative">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <i class="fa fa-lock text-gray-400"></i>
                            </div>
                            <input type="password" id="confirmPassword" class="pl-10 pr-4 py-3 w-full border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary/50 focus:border-primary outline-none transition" placeholder="请再次输入密码" required>
                        </div>
                        <!-- 密码不一致错误提示 -->
                        <p id="passwordMismatchError" class="text-red-500 text-sm mt-1 hidden text-left"></p>
                    </div>

                    <!-- 注册按钮 -->
                    <button type="submit" id="registerBtn" class="w-full bg-primary text-white font-medium py-3 px-4 rounded-lg hover:bg-primary/90 focus:outline-none focus:ring-2 focus:ring-primary/50 transition">
                        <i class="fa fa-user-plus mr-2"></i> 注册
                    </button>

                    <!-- 登录链接 -->
                    <div class="text-center text-sm text-gray-500 mt-4">
                        已有账号? <a href="login.html" class="text-primary hover:underline">立即登录</a>
                    </div>
                </form>
            </div>

            <!-- 底部版权 -->
            <div class="text-center text-gray-400 text-sm mt-8">
                
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            // 表单提交处理
            $('#registerForm').on('submit', function(e) {
                e.preventDefault();

                // 隐藏之前的提示信息
                $('#generalMessage').addClass('hidden');
                $('#generalMessageText').text('').removeClass('text-green-700 text-red-700 bg-green-100 bg-red-100');
                $('#passwordMismatchError').addClass('hidden').text('');

                // 获取表单数据
                const username = $('#username').val().trim();
                const password = $('#password').val();
                const confirmPassword = $('#confirmPassword').val();

                // 简单验证（确保密码一致）
                if (password !== confirmPassword) {
                    $('#passwordMismatchError').text('两次输入的密码不一致').removeClass('hidden');
                    return;
                }

                // 显示加载中
                $('#loadingOverlay').removeClass('hidden');

                // 构建请求数据
                const requestData = {
                    username: username,
                    password: password,
                    role: "student" // 默认角色为学生
                };

                // 发送AJAX请求
                $.ajax({
                    url: 'http://localhost:8082/api/register',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(requestData),
                    success: function(response) {
                        // 隐藏加载中
                        $('#loadingOverlay').addClass('hidden');

                        if (response.code === 200) {
                            // 注册成功，显示成功提示并跳转到登录页
                            $('#generalMessageText').text('注册成功，请使用新账号登录').addClass('text-green-700');
                            $('#generalMessage div').addClass('bg-green-100');
                            $('#generalMessage').removeClass('hidden');

                            // 延时跳转到登录页
                            setTimeout(function() {
                                window.location.href = 'login.html';
                            }, 2000); // 2秒后跳转

                        } else {
                            // 注册失败，显示错误信息提示
                            $('#generalMessageText').text(response.msg || '注册失败，请稍后重试').addClass('text-red-700');
                            $('#generalMessage div').addClass('bg-red-100');
                            $('#generalMessage').removeClass('hidden');
                        }
                    },
                    error: function() {
                        // 隐藏加载中
                        $('#loadingOverlay').addClass('hidden');

                        console.error('注册请求失败:', error);
                        $('#generalMessageText').text('服务器错误，请稍后重试').addClass('text-red-700');
                        $('#generalMessage div').addClass('bg-red-100');
                        $('#generalMessage').removeClass('hidden');
                    }
                });
            });
        });
    </script>
</body>

</html>